/**
 * Created with IntelliJ IDEA.
 * Description:删除有序链表中的重复元素-1
 * User: wangxin
 * Date: 2024-10-30
 * Time: 19:19
 */
import java.util.*;


   class ListNode1 {
    int val;
    ListNode next = null;
    public ListNode1(int val) {
      this.val = val;
    }
  }

public class test2 {

//方法一

        public ListNode deleteDuplicates (ListNode head) {
            // write code here
            if(head == null || head.next == null){
                return head;
            }
            ListNode cur = head;
            ListNode pre = head;
            while(cur.next != null){
                if(cur.val != cur.next.val){
                    pre = cur;
                    cur = cur.next;
                }else{
                    if(cur == head){
                        head = cur = cur.next;
                    }else{
                        ListNode curn = cur.next;
                        pre.next = curn;

                        cur.next = null;
                        cur = curn;
                    }
                }
            }
            return head;
        }
        //方法二
        public ListNode deleteDuplicates2 (ListNode head) {
            //空链表
            if(head == null)
                return null;
            //遍历指针
            ListNode cur = head;
            //指针当前和下一位不为空
            while(cur != null && cur.next != null){
                //如果当前与下一位相等则忽略下一位
                if(cur.val == cur.next.val)
                    cur.next = cur.next.next;
                    //否则指针正常遍历
                else
                    cur = cur.next;
            }
            return head;
        }

}
